﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;

namespace DongFangGuoKai.WMSPlatform.WebApi.Utility.Filters
{
    /// <summary>
    /// IP安全列表筛选器
    /// [ServiceFilter] 属性的控制器或操作方法   
    /// </summary>
    public class ClientIpCheckActionAsyncFilter : Attribute, IAsyncActionFilter
    {
        private readonly ILogger _logger;
        private readonly string _safelist;
        public ClientIpCheckActionAsyncFilter(string safelist, ILogger logger)
        {
            _safelist = safelist;
            _logger = logger;
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var remoteIp = context.HttpContext.Connection.RemoteIpAddress;
            _logger.LogDebug("Remote IpAddress: {RemoteIp}", remoteIp);
            var ip = _safelist.Split(';');
            var badIp = true;
            if (remoteIp.IsIPv4MappedToIPv6)
            {
                remoteIp = remoteIp.MapToIPv4();
            }
            foreach (var address in ip)
            {
                var testIp = IPAddress.Parse(address);

                if (testIp.Equals(remoteIp))
                {
                    badIp = false;
                    break;
                }
            }   
            if (badIp)
            {
                _logger.LogWarning("Forbidden Request from IP: {RemoteIp}", remoteIp);
                context.Result = new StatusCodeResult(StatusCodes.Status403Forbidden);
                return;
            }
            else
            {
                await next.Invoke();
            }

        }
    }
}
